package com.shiyipicture.shiyipicturebackend.controller;

import com.shiyipicture.shiyipicturebackend.annotation.AuthCheck;
import com.shiyipicture.shiyipicturebackend.auth.SpaceUserAuthManager;
import com.shiyipicture.shiyipicturebackend.common.BaseResponse;
import com.shiyipicture.shiyipicturebackend.common.ResultUtils;
import com.shiyipicture.shiyipicturebackend.constant.UserConstant;
import com.shiyipicture.shiyipicturebackend.enums.SpaceLevelEnum;
import com.shiyipicture.shiyipicturebackend.exception.BusinessException;
import com.shiyipicture.shiyipicturebackend.exception.ErrorCode;
import com.shiyipicture.shiyipicturebackend.exception.ThrowUtils;
import com.shiyipicture.shiyipicturebackend.model.dto.space.SpaceAddRequest;
import com.shiyipicture.shiyipicturebackend.model.dto.space.SpaceUpdateRequest;
import com.shiyipicture.shiyipicturebackend.model.entity.Space;
import com.shiyipicture.shiyipicturebackend.model.entity.User;
import com.shiyipicture.shiyipicturebackend.model.vo.SpaceLevel;
import com.shiyipicture.shiyipicturebackend.model.vo.SpaceVO;
import com.shiyipicture.shiyipicturebackend.service.SpaceService;
import com.shiyipicture.shiyipicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/space")
public class SpaceController {

  @Resource
  private SpaceService spaceService;

  @Resource
  private UserService userService;

  @Resource
  private SpaceUserAuthManager spaceUserAuthManager;

  /**
   * 创建空间
   */
  @PostMapping("/add")
  public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest,HttpServletRequest request) {
    ThrowUtils.throwIf(spaceAddRequest == null, ErrorCode.PARAMS_ERROR);
    User loginUser = userService.getLoginUser(request);
    long result = spaceService.addSpace(spaceAddRequest, loginUser);
    return ResultUtils.success(result);
  }

  /**
   * 更新空间
   */
  @PostMapping("/update")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  public BaseResponse<Boolean> updatePicture(@RequestBody SpaceUpdateRequest spaceUpdateRequest) {
    if (spaceUpdateRequest == null || spaceUpdateRequest.getId() < 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Space space = new Space();
    //数据转换
    BeanUtils.copyProperties(spaceUpdateRequest,space);
    spaceService.validSpace(space,false);
    // 判断是否存在
    Space oldSpace = spaceService.getById(space);
    ThrowUtils.throwIf(oldSpace == null,ErrorCode.NOT_FOUND_ERROR);

    // 填充数据
    spaceService.fillSpaceBySpaceLevel(space);
    //操作数据库
    boolean result = spaceService.updateById(space);
    ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
    return ResultUtils.success(true);
  }

  /**
   * 获取空间封装类
   * @param id
   * @param request
   * @return
   */
  @GetMapping("/get/vo")
  public BaseResponse<SpaceVO> getSpaceVOById(long id, HttpServletRequest request) {
    ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
    // 查询数据库
    Space space = spaceService.getById(id);
    ThrowUtils.throwIf(space == null, ErrorCode.NOT_FOUND_ERROR);
    SpaceVO spaceVO = spaceService.getSpaceVO(space, request);
    User loginUser = userService.getLoginUser(request);
    List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
    spaceVO.setPermissionList(permissionList);
    // 获取封装类
    return ResultUtils.success(spaceVO);
  }


  @GetMapping("/list/level")
  public BaseResponse<List<SpaceLevel>> listSpaceLevel(){
    List<SpaceLevel> spaceLevelList = Arrays.stream(SpaceLevelEnum.values()).map(spaceLevelEnum -> new SpaceLevel(
      spaceLevelEnum.getValue(),
      spaceLevelEnum.getText(),
      spaceLevelEnum.getMaxSize(),
      spaceLevelEnum.getMaxCount()
    )).collect(Collectors.toList());
    return ResultUtils.success(spaceLevelList);
  }
}
